package com.sky.service.impl;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.sky.exception.BaseException;
import com.sky.properties.WeChatProperties;
import com.sky.service.WechatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author zzw
 * @date 2025/6/4
 * @description 微信接口调用实现
 */
@Service
public class WechatServiceImpl implements WechatService {

    @Autowired
    private WeChatProperties weChatProperties;


    // 登录
    private static final String REQUEST_URL = "https://api.weixin.qq.com/sns/jscode2session?grant_type" +
                                              "=authorization_code";

    // 获取token
    private static final String TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential";

    // 获取手机号
    private static final String PHONE_REQUEST_URL = "https://api.weixin.qq.com/wxa/business/getuserphonenumber" +
                                                    "?access_token=";

    /**
     * 获取openid
     *
     * @param code 微信登录成功后返回的登录凭证
     * @return openid
     */
    @Override
    public String getOpenid(String code) {
        // 1.构造请求参数
        Map<String, Object> params = new HashMap<>();
        params.put("appid", weChatProperties.getAppid());
        params.put("secret", weChatProperties.getSecret());
        params.put("js_code", code);

        // 2.发送请求
        String result = HttpUtil.get(REQUEST_URL, params);

        // 3.解析结果
        JSONObject jsonObject = JSONUtil.parseObj(result);
        if (jsonObject.getInt("errcode") != null) {
            throw new BaseException(jsonObject.getStr("errmsg"));
        }
        return jsonObject.getStr("openid");
    }

    /**
     * 获取微信用户的手机号
     *
     * @param phoneCode 微信登录成功后返回的参数
     * @return 手机号
     */
    @Override
    public String getPhone(String phoneCode) {
        // 1.获取token
        String token = getToken();

        // 2.构建参数
        Map<String, Object> params = new HashMap<>();
        params.put("code", phoneCode);

        // 3.发送请求
        String result = HttpUtil.post(PHONE_REQUEST_URL + token, JSONUtil.toJsonStr(params));

        // 4.解析结果
        JSONObject jsonObject = JSONUtil.parseObj(result);
        if (jsonObject.getInt("errcode") != 0) {
            throw new BaseException(jsonObject.getStr("errmsg"));
        }
        return jsonObject.getJSONObject("phone_info").getStr("phoneNumber");
    }

    /**
     * 获取token
     *
     * @return token
     */
    private String getToken() {
        Map<String, Object> params = new HashMap<>();
        params.put("appid", weChatProperties.getAppid());
        params.put("secret", weChatProperties.getSecret());

        String result = HttpUtil.get(TOKEN_URL, params);
        JSONObject jsonObject = JSONUtil.parseObj(result);
        if (jsonObject.getInt("errcode") != null) {
            throw new BaseException(jsonObject.getStr("errmsg"));
        }

        return jsonObject.getStr("access_token");
    }
}
